package losrebellos.papervision3d.view 
{
	import losrebellos.utils.ArrayUtils;

	import org.papervision3d.objects.DisplayObject3D;

	import flash.events.Event;

	/*
	 *
	 * @author los rebellos - ben@benoitvinay.com
	 *
	 */
	public class Viewport3DPlus extends AbstractViewport3DPlus 
	{
		/*
		 *
		 * CONSTRUCTOR
		 *
		 */
		public function Viewport3DPlus(viewportWidth:Number = 640, viewportHeight:Number = 480, autoScaleToStage:Boolean = false, interactive:Boolean = false, autoClipping:Boolean = true, autoCulling:Boolean = true)
		{
			super(viewportWidth, viewportHeight, autoScaleToStage, interactive, autoClipping, autoCulling);
			
			createScene();
			createCamera();
			createLight();
			createRenderer();
			
			createContent();
		}
		
		
		/*
		 * 
		 * CHILD 3D
		 * 
		 */
		override public function getChild3DByName(name:String, recursive:Boolean = false):DisplayObject3D
		{
			return _scene.getChildByName(name, recursive);
		}
		override public function addChild3D(child:DisplayObject3D, name:String = null):DisplayObject3D
		{
			if((name && _children.indexOf(name) == -1) || _children.indexOf(child.name) == -1)
			{
				_children.push(name);
				
				return _scene.addChild(child, name);
			}
			
			return null;
		}
		override public function removeChild3D(child:DisplayObject3D):DisplayObject3D
		{
			var index:int = _children.indexOf(child.name);
			if(index != -1)
			{
				ArrayUtils.remove(_children, index);
				
				return _scene.removeChild(child);
			}
			
			return null;
		}
		override public function removeChild3DByName(name:String):DisplayObject3D
		{
			var index:int = _children.indexOf(name);
			if(index != -1)
			{
				ArrayUtils.remove(_children, index);
				
				return _scene.removeChildByName(name);
			}
			
			return null;
		}
		override public function get numChildren3D():int
		{
			return _children.length;
		}

		
		/*
		 * 
		 * RENDERING
		 * 
		 */
		override public function startRendering():void
		{
			this.addEventListener(Event.ENTER_FRAME, render);
		}
		override public function stopRendering():void
		{
			this.removeEventListener(Event.ENTER_FRAME, render);
		}
		
		
		/*
		 * 
		 * UTILS
		 * 
		 */
		override public function getPixelZ():Number
		{
			return (_camera.zoom * _camera.focus) - Math.abs(_camera.z);
		}
	}
}